﻿using System;
using System.Data;
using System.Collections.Generic;

using BrnMall.Core;
using BrnMall.Manager.Domain.Store;
using BrnMall.Manager.Manager.Store;

namespace BrnMall.Services
{
    /// <summary>
    /// 积分操作管理类
    /// </summary>
    public partial  class Credits
    {
        private static object _locker = new object();//锁对象


        /// <summary>
        /// 发放登录积分
        /// </summary>
        /// <param name="storeId">店铺ID</param>
        /// <param name="partUserInfo">用户信息</param>
        /// <param name="loginTime">登录时间</param>
        public static void SendLoginCredits(int storeId,ref PartUserInfo partUserInfo, DateTime loginTime)
        {
            DateTime oldTime = TypeHelper.StringToDateTime(WebHelper.UrlDecode(MallUtils.GetBMACookie("slctime")), loginTime.Date.AddDays(-2));
            DateTime newTime;
            SendLoginCredits(storeId,ref partUserInfo, loginTime, oldTime, out newTime);

            if (newTime == loginTime)
                MallUtils.SetBMACookie("slctime", WebHelper.UrlEncode(loginTime.ToString()));
        }

        /// <summary>
        /// 发放登录积分
        /// </summary>
        /// <param name="storeId">店铺ID</param>
        /// <param name="partUserInfo">用户信息</param>
        /// <param name="loginTime">登录时间</param>
        /// <param name="oldTime">上次发放登录积分时间</param>
        /// <param name="newTime">本次发放登录积分时间</param>
        public static void SendLoginCredits(int storeId,ref PartUserInfo partUserInfo, DateTime loginTime, DateTime oldTime, out DateTime newTime)
        {
            newTime = oldTime;
            var creditconfiginfo = StoreConfigManager.GetStoreCreditConfigInfo(storeId);
            if (creditconfiginfo == null) return;
            if (creditconfiginfo.LoginPayCredits > 0 || creditconfiginfo.LoginRankCredits > 0)
            {
                if (loginTime.Date <= oldTime.Date)
                    return;

                if (!IsSendTodayLoginCredit(partUserInfo.Uid, DateTime.Now))
                {
                    newTime = loginTime;

                    int surplusPayCredits = GetDaySurplusPayCredits(creditconfiginfo,partUserInfo.Uid, loginTime.Date);
                    int surplusRankCredits = GetDaySurplusRankCredits(creditconfiginfo,partUserInfo.Uid, loginTime.Date);
                    if (surplusPayCredits == 0 && surplusRankCredits == 0)
                        return;

                    int payCredits = 0;
                    int rankCredits = 0;
                    if (surplusPayCredits > 0)
                        payCredits = surplusPayCredits < creditconfiginfo.LoginPayCredits ? surplusPayCredits : creditconfiginfo.LoginPayCredits;
                    else if (surplusPayCredits == -1)
                        payCredits = creditconfiginfo.LoginPayCredits;
                    if (surplusRankCredits > 0)
                        rankCredits = surplusRankCredits < creditconfiginfo.LoginRankCredits ? surplusRankCredits : creditconfiginfo.LoginRankCredits;
                    else if (surplusRankCredits == -1)
                        rankCredits = creditconfiginfo.LoginRankCredits;

                    partUserInfo.PayCredits += payCredits;
                    partUserInfo.RankCredits += rankCredits;

                    partUserInfo.UserRid = UserRanks.GetUserRankByCredits(partUserInfo.RankCredits,partUserInfo.RegisterFromStoreId)?.UserRid??0;

                    CreditLogInfo creditLogInfo = new CreditLogInfo();
                    creditLogInfo.Uid = partUserInfo.Uid;
                    creditLogInfo.PayCredits = payCredits;
                    creditLogInfo.RankCredits = rankCredits;
                    creditLogInfo.Action = (int)CreditAction.Login;
                    creditLogInfo.ActionCode = 0;
                    creditLogInfo.ActionTime = loginTime;
                    creditLogInfo.ActionDes = "登录赠送积分";
                    creditLogInfo.Operator = 0;

                    SendCredits(partUserInfo.UserRid, creditLogInfo);
                }
            }
        }

        /// <summary>
        /// 发放注册积分
        /// </summary>
        /// <param name="storeId">店铺ID</param>
        /// <param name="userInfo">用户信息</param>
        /// <param name="registerTime">注册时间</param>
        public static void SendRegisterCredits(int storeId, ref UserInfo userInfo, DateTime registerTime)
        {
            var creditconfiginfo = StoreConfigManager.GetStoreCreditConfigInfo(storeId);
            if (creditconfiginfo == null) return;
            if (creditconfiginfo.RegisterPayCredits > 0 || creditconfiginfo.RegisterRankCredits > 0)
            {
                int surplusPayCredits = GetDaySurplusPayCredits(creditconfiginfo,userInfo.Uid, registerTime.Date);
                int surplusRankCredits = GetDaySurplusRankCredits(creditconfiginfo,userInfo.Uid, registerTime.Date);
                if (surplusPayCredits == 0 && surplusRankCredits == 0)
                    return;

                int payCredits = 0;
                int rankCredits = 0;
                if (surplusPayCredits > 0)
                    payCredits = surplusPayCredits < creditconfiginfo.RegisterPayCredits ? surplusPayCredits : creditconfiginfo.RegisterPayCredits;
                else if (surplusPayCredits == -1)
                    payCredits = creditconfiginfo.RegisterPayCredits;
                if (surplusRankCredits > 0)
                    rankCredits = surplusRankCredits < creditconfiginfo.RegisterRankCredits ? surplusRankCredits : creditconfiginfo.RegisterRankCredits;
                else if (surplusRankCredits == -1)
                    rankCredits = creditconfiginfo.RegisterRankCredits;

                userInfo.PayCredits += payCredits;
                userInfo.RankCredits += rankCredits;

                userInfo.UserRid = UserRanks.GetUserRankByCredits(userInfo.RankCredits,userInfo.RegisterFromStoreId)?.UserRid??0;

                CreditLogInfo creditLogInfo = new CreditLogInfo();
                creditLogInfo.Uid = userInfo.Uid;
                creditLogInfo.PayCredits = payCredits;
                creditLogInfo.RankCredits = rankCredits;
                creditLogInfo.Action = (int)CreditAction.Register;
                creditLogInfo.ActionCode = 0;
                creditLogInfo.ActionTime = registerTime;
                creditLogInfo.ActionDes = "注册赠送积分";
                creditLogInfo.Operator = 0;

                SendCredits(userInfo.UserRid, creditLogInfo);
            }
        }

        /// <summary>
        /// 发放验证邮箱积分
        /// </summary>
        /// <param name="partUserInfo">用户信息</param>
        /// <param name="verifyTime">验证时间</param>
        public static void SendVerifyEmailCredits(int storeId, ref PartUserInfo partUserInfo, DateTime verifyTime)
        {
            var creditconfiginfo = StoreConfigManager.GetStoreCreditConfigInfo(storeId);
            if (creditconfiginfo == null) return;
            if (partUserInfo.VerifyEmail == 0 && (creditconfiginfo.VerifyEmailPayCredits > 0 || creditconfiginfo.VerifyEmailRankCredits > 0))
            {
                int surplusPayCredits = GetDaySurplusPayCredits(creditconfiginfo, partUserInfo.Uid, verifyTime.Date);
                int surplusRankCredits = GetDaySurplusRankCredits(creditconfiginfo, partUserInfo.Uid, verifyTime.Date);
                if (surplusPayCredits == 0 && surplusRankCredits == 0)
                    return;

                int payCredits = 0;
                int rankCredits = 0;
                if (surplusPayCredits > 0)
                    payCredits = surplusPayCredits < creditconfiginfo.VerifyEmailPayCredits ? surplusPayCredits : creditconfiginfo.VerifyEmailPayCredits;
                else if (surplusPayCredits == -1)
                    payCredits = creditconfiginfo.VerifyEmailPayCredits;
                if (surplusRankCredits > 0)
                    rankCredits = surplusRankCredits < creditconfiginfo.VerifyEmailRankCredits ? surplusRankCredits : creditconfiginfo.VerifyEmailRankCredits;
                else if (surplusRankCredits == -1)
                    rankCredits = creditconfiginfo.VerifyEmailRankCredits;

                partUserInfo.PayCredits += payCredits;
                partUserInfo.RankCredits += rankCredits;

                partUserInfo.UserRid = UserRanks.GetUserRankByCredits(partUserInfo.RankCredits,partUserInfo.RegisterFromStoreId)?.UserRid??0;

                CreditLogInfo creditLogInfo = new CreditLogInfo();
                creditLogInfo.Uid = partUserInfo.Uid;
                creditLogInfo.PayCredits = payCredits;
                creditLogInfo.RankCredits = rankCredits;
                creditLogInfo.Action = (int)CreditAction.VerifyEmail;
                creditLogInfo.ActionCode = 0;
                creditLogInfo.ActionTime = verifyTime;
                creditLogInfo.ActionDes = "验证用户邮箱";
                creditLogInfo.Operator = 0;

                SendCredits(partUserInfo.UserRid, creditLogInfo);
            }
        }

        /// <summary>
        /// 发放验证手机积分
        /// </summary>
        /// <param name="partUserInfo">用户信息</param>
        /// <param name="verifyTime">验证时间</param>
        public static void SendVerifyMobileCredits(int storeId, ref PartUserInfo partUserInfo, DateTime verifyTime)
        {
            var creditconfiginfo = StoreConfigManager.GetStoreCreditConfigInfo(storeId);
            if (creditconfiginfo == null) return;
            if (partUserInfo.VerifyMobile == 0 && (creditconfiginfo.VerifyMobilePayCredits > 0 || creditconfiginfo.VerifyMobileRankCredits > 0))
            {
                int surplusPayCredits = GetDaySurplusPayCredits(creditconfiginfo, partUserInfo.Uid, verifyTime.Date);
                int surplusRankCredits = GetDaySurplusRankCredits(creditconfiginfo, partUserInfo.Uid, verifyTime.Date);
                if (surplusPayCredits == 0 && surplusRankCredits == 0)
                    return;

                int payCredits = 0;
                int rankCredits = 0;
                if (surplusPayCredits > 0)
                    payCredits = surplusPayCredits < creditconfiginfo.VerifyMobilePayCredits ? surplusPayCredits : creditconfiginfo.VerifyMobilePayCredits;
                else if (surplusPayCredits == -1)
                    payCredits = creditconfiginfo.VerifyMobilePayCredits;
                if (surplusRankCredits > 0)
                    rankCredits = surplusRankCredits < creditconfiginfo.VerifyMobileRankCredits ? surplusRankCredits : creditconfiginfo.VerifyMobileRankCredits;
                else if (surplusRankCredits == -1)
                    rankCredits = creditconfiginfo.VerifyMobileRankCredits;

                partUserInfo.PayCredits += payCredits;
                partUserInfo.RankCredits += rankCredits;

                partUserInfo.UserRid = UserRanks.GetUserRankByCredits(partUserInfo.RankCredits,partUserInfo.RegisterFromStoreId)?.UserRid??0;

                CreditLogInfo creditLogInfo = new CreditLogInfo();
                creditLogInfo.Uid = partUserInfo.Uid;
                creditLogInfo.PayCredits = payCredits;
                creditLogInfo.RankCredits = rankCredits;
                creditLogInfo.Action = (int)CreditAction.VerifyMobile;
                creditLogInfo.ActionCode = 0;
                creditLogInfo.ActionTime = verifyTime;
                creditLogInfo.ActionDes = "验证用户手机";
                creditLogInfo.Operator = 0;

                SendCredits(partUserInfo.UserRid, creditLogInfo);
            }
        }

        /// <summary>
        /// 发放完善用户信息积分
        /// </summary>
        /// <param name="partUserInfo">用户信息</param>
        /// <param name="completeTime">完成时间</param>
        public static void SendCompleteUserInfoCredits(int storeId, ref PartUserInfo partUserInfo, DateTime completeTime)
        {
            var creditconfiginfo = StoreConfigManager.GetStoreCreditConfigInfo(storeId);
            if (creditconfiginfo == null) return;
            if ((creditconfiginfo.CompleteUserInfoPayCredits > 0 || creditconfiginfo.CompleteUserInfoRankCredits > 0) && !IsSendCompleteUserInfoCredit(partUserInfo.Uid))
            {
                int surplusPayCredits = GetDaySurplusPayCredits(creditconfiginfo, partUserInfo.Uid, completeTime.Date);
                int surplusRankCredits = GetDaySurplusRankCredits(creditconfiginfo, partUserInfo.Uid, completeTime.Date);
                if (surplusPayCredits == 0 && surplusRankCredits == 0)
                    return;

                int payCredits = 0;
                int rankCredits = 0;
                if (surplusPayCredits > 0)
                    payCredits = surplusPayCredits < creditconfiginfo.CompleteUserInfoPayCredits ? surplusPayCredits : creditconfiginfo.CompleteUserInfoPayCredits;
                else if (surplusPayCredits == -1)
                    payCredits = creditconfiginfo.CompleteUserInfoPayCredits;
                if (surplusRankCredits > 0)
                    rankCredits = surplusRankCredits < creditconfiginfo.CompleteUserInfoRankCredits ? surplusRankCredits : creditconfiginfo.CompleteUserInfoRankCredits;
                else if (surplusRankCredits == -1)
                    rankCredits = creditconfiginfo.CompleteUserInfoRankCredits;

                partUserInfo.PayCredits += payCredits;
                partUserInfo.RankCredits += rankCredits;

                partUserInfo.UserRid = UserRanks.GetUserRankByCredits(partUserInfo.RankCredits,partUserInfo.RegisterFromStoreId)?.UserRid??0;

                CreditLogInfo creditLogInfo = new CreditLogInfo();
                creditLogInfo.Uid = partUserInfo.Uid;
                creditLogInfo.PayCredits = payCredits;
                creditLogInfo.RankCredits = rankCredits;
                creditLogInfo.Action = (int)CreditAction.CompleteUserInfo;
                creditLogInfo.ActionCode = 0;
                creditLogInfo.ActionTime = completeTime;
                creditLogInfo.ActionDes = "完善用户信息";
                creditLogInfo.Operator = 0;

                SendCredits(partUserInfo.UserRid, creditLogInfo);
            }
        }

        /// <summary>
        /// 发放收货积分
        /// </summary>
        /// <param name="partUserInfo">用户信息</param>
        /// <param name="orderInfo">订单信息</param>
        /// <param name="orderProductList">订单商品列表</param>
        /// <param name="receiveTime">收货时间</param>
        public static void SendReceiveOrderCredits(int storeId, ref PartUserInfo partUserInfo, OrderInfo orderInfo, List<OrderProductInfo> orderProductList, DateTime receiveTime)
        {
            var creditconfiginfo = StoreConfigManager.GetStoreCreditConfigInfo(storeId);
            if (creditconfiginfo == null) return;
            if (creditconfiginfo.ReceiveOrderPayCredits > 0 || creditconfiginfo.ReceiveOrderRankCredits > 0)
            {
                int surplusPayCredits = GetDaySurplusPayCredits(creditconfiginfo, partUserInfo.Uid, receiveTime.Date);
                int surplusRankCredits = GetDaySurplusRankCredits(creditconfiginfo, partUserInfo.Uid, receiveTime.Date);
                if (surplusPayCredits == 0 && surplusRankCredits == 0)
                    return;

                int payCredits = 0;
                int rankCredits = 0;
                int tempPayCredits = (int)Math.Floor(orderInfo.OrderAmount * creditconfiginfo.ReceiveOrderPayCredits / 100);
                int tempRankCredits = (int)Math.Floor(orderInfo.OrderAmount * creditconfiginfo.ReceiveOrderRankCredits / 100);
                if (surplusPayCredits > 0)
                    payCredits = surplusPayCredits < tempPayCredits ? surplusPayCredits : tempPayCredits;
                else if (surplusPayCredits == -1)
                    payCredits = tempPayCredits;
                if (surplusRankCredits > 0)
                    rankCredits = surplusRankCredits < tempRankCredits ? surplusRankCredits : tempRankCredits;
                else if (surplusRankCredits == -1)
                    rankCredits = tempRankCredits;

                partUserInfo.PayCredits += payCredits;
                partUserInfo.RankCredits += rankCredits;

                partUserInfo.UserRid = UserRanks.GetUserRankByCredits(partUserInfo.RankCredits,partUserInfo.RegisterFromStoreId)?.UserRid??0;

                CreditLogInfo creditLogInfo = new CreditLogInfo();
                creditLogInfo.Uid = partUserInfo.Uid;
                creditLogInfo.PayCredits = payCredits;
                creditLogInfo.RankCredits = rankCredits;
                creditLogInfo.Action = (int)CreditAction.CompleteOrder;
                creditLogInfo.ActionCode = orderInfo.Oid;
                creditLogInfo.ActionTime = receiveTime;
                creditLogInfo.ActionDes = "收货:" + orderInfo.OSN;
                creditLogInfo.Operator = 0;

                SendCredits(partUserInfo.UserRid, creditLogInfo);
            }
        }

        /// <summary>
        /// 发放评价商品积分
        /// </summary>
        /// <param name="partUserInfo">用户信息</param>
        /// <param name="orderProductInfo">订单商品</param>
        /// <param name="reviewTime">评价时间</param>
        public static int SendReviewProductCredits(int storeId, ref PartUserInfo partUserInfo, OrderProductInfo orderProductInfo, DateTime reviewTime)
        {
            var creditconfiginfo = StoreConfigManager.GetStoreCreditConfigInfo(storeId);
            if (creditconfiginfo == null) return 0;
            if (creditconfiginfo.ReviewProductPayCredits > 0 || creditconfiginfo.ReviewProductRankCredits > 0)
            {
                int surplusPayCredits = GetDaySurplusPayCredits(creditconfiginfo, partUserInfo.Uid, reviewTime.Date);
                int surplusRankCredits = GetDaySurplusRankCredits(creditconfiginfo, partUserInfo.Uid, reviewTime.Date);
                if (surplusPayCredits == 0 && surplusRankCredits == 0)
                    return 0;

                int payCredits = 0;
                int rankCredits = 0;
                if (surplusPayCredits > 0)
                    payCredits = surplusPayCredits < creditconfiginfo.ReviewProductPayCredits ? surplusPayCredits : creditconfiginfo.ReviewProductPayCredits;
                else if (surplusPayCredits == -1)
                    payCredits = creditconfiginfo.ReviewProductPayCredits;
                if (surplusRankCredits > 0)
                    rankCredits = surplusRankCredits < creditconfiginfo.ReviewProductRankCredits ? surplusRankCredits : creditconfiginfo.ReviewProductRankCredits;
                else if (surplusRankCredits == -1)
                    rankCredits = creditconfiginfo.ReviewProductRankCredits;

                partUserInfo.PayCredits += payCredits;
                partUserInfo.RankCredits += rankCredits;

                partUserInfo.UserRid = UserRanks.GetUserRankByCredits(partUserInfo.RankCredits,partUserInfo.RegisterFromStoreId)?.UserRid??0;

                CreditLogInfo creditLogInfo = new CreditLogInfo();
                creditLogInfo.Uid = partUserInfo.Uid;
                creditLogInfo.PayCredits = payCredits;
                creditLogInfo.RankCredits = rankCredits;
                creditLogInfo.Action = (int)CreditAction.ReviewProduct;
                creditLogInfo.ActionCode = orderProductInfo.Oid;
                creditLogInfo.ActionTime = reviewTime;
                creditLogInfo.ActionDes = "评价商品:" + orderProductInfo.Name;
                creditLogInfo.Operator = 0;

                SendCredits(partUserInfo.UserRid, creditLogInfo);

                return payCredits;
            }
            return 0;
        }

        /// <summary>
        /// 发放单品促销活动积分
        /// </summary>
        /// <param name="partUserInfo">用户信息</param>
        /// <param name="orderInfo">订单信息</param>
        /// <param name="credits">积分</param>
        /// <param name="sendTime">发放时间</param>
        public static void SendSinglePromotionCredits(int storeId, ref PartUserInfo partUserInfo, OrderInfo orderInfo, int credits, DateTime sendTime)
        {

            int surplusPayCredits = GetDaySurplusPayCredits(StoreConfigManager.GetStoreCreditConfigInfo(storeId), partUserInfo.Uid, sendTime.Date);
            if (surplusPayCredits != 0)
            {
                int creditCount = credits;
                if (surplusPayCredits > 0 && surplusPayCredits < creditCount)
                    creditCount = surplusPayCredits;

                partUserInfo.PayCredits += creditCount;

                var creditLogInfo = new CreditLogInfo
                {
                    Uid = partUserInfo.Uid,
                    PayCredits = creditCount,
                    RankCredits = 0,
                    Action = (int) CreditAction.SinglePromotion,
                    ActionCode = orderInfo.Oid,
                    ActionTime = sendTime,
                    ActionDes = "促销活动发放",
                    Operator = 0
                };

                SendPayCredits(creditLogInfo);
            }
        }

        /// <summary>
        /// 支付订单
        /// </summary>
        /// <param name="partUserInfo">用户信息</param>
        /// <param name="orderInfo">订单信息</param>
        /// <param name="credits">积分数量</param>
        /// <param name="payTime">支付时间</param>
        public static void PayOrder(ref PartUserInfo partUserInfo, OrderInfo orderInfo, int credits, DateTime payTime)
        {
            if (credits > 0)
            {
                partUserInfo.PayCredits -= credits;

                var creditLogInfo = new CreditLogInfo
                {
                    Uid = partUserInfo.Uid,
                    PayCredits = -1 * credits,
                    RankCredits = 0,
                    Action = (int) CreditAction.PayOrder,
                    ActionCode = orderInfo.Oid,
                    ActionTime = payTime,
                    ActionDes = "支付订单:" + orderInfo.OSN,
                    Operator = partUserInfo.Uid
                };

                SendPayCredits(creditLogInfo);
            }
        }
        /// <summary>
        /// 支付当面付订单
        /// </summary>
        /// <param name="partUserInfo">用户信息</param>
        /// <param name="orderInfo">订单信息</param>
        /// <param name="credits">积分数量</param>
        /// <param name="payTime">支付时间</param>
        public static void PayDirectPayment(int uid, int pid,string psn, int credits, DateTime payTime)
        {
            if (credits > 0)
            {
               

                var creditLogInfo = new CreditLogInfo
                {
                    Uid = uid,
                    PayCredits = -1 * credits,
                    RankCredits = 0,
                    Action = (int)CreditAction.DirectPayment,
                    ActionCode = pid,
                    ActionTime = payTime,
                    ActionDes = "支付当面付订单:" + psn,
                    Operator = uid 
                };

                SendPayCredits(creditLogInfo);
            }
        }

        /// <summary>
        /// 退回用户订单使用的积分
        /// </summary>
        /// <param name="partUserInfo">用户信息</param>
        /// <param name="orderInfo">订单信息</param>
        /// <param name="operatorId">操作人</param>
        /// <param name="returnTime">退回时间</param>
        public static void ReturnUserOrderUseCredits(ref PartUserInfo partUserInfo, OrderInfo orderInfo, int operatorId, DateTime returnTime)
        {
            partUserInfo.PayCredits += orderInfo.PayCreditCount;

            var creditLogInfo = new CreditLogInfo
            {
                Uid = orderInfo.Uid,
                PayCredits = orderInfo.PayCreditCount,
                RankCredits = 0,
                Action = (int) CreditAction.ReturnOrderUse,
                ActionCode = orderInfo.Oid,
                ActionTime = returnTime,
                ActionDes = "退回用户订单使用的积分:" + orderInfo.OSN,
                Operator = operatorId
            };

            SendPayCredits(creditLogInfo);
        }

        /// <summary>
        /// 退回用户订单发放的积分
        /// </summary>
        /// <param name="partUserInfo">用户信息</param>
        /// <param name="orderInfo">订单信息</param>
        /// <param name="payCredits">支付积分</param>
        /// <param name="rankCredits">等级积分</param>
        /// <param name="operatorId">操作人</param>
        /// <param name="returnTime">退回时间</param>
        public static void ReturnUserOrderSendCredits(ref PartUserInfo partUserInfo, OrderInfo orderInfo, int payCredits, int rankCredits, int operatorId, DateTime returnTime)
        {
            partUserInfo.PayCredits -= payCredits;
            partUserInfo.RankCredits -= rankCredits;

            partUserInfo.UserRid = UserRanks.GetUserRankByCredits(partUserInfo.RankCredits,partUserInfo.RegisterFromStoreId)?.UserRid??0;

            var creditLogInfo = new CreditLogInfo
            {
                Uid = orderInfo.Uid,
                PayCredits = -1 * payCredits,
                RankCredits = -1 * rankCredits,
                Action = (int) CreditAction.ReturnOrderSend,
                ActionCode = orderInfo.Oid,
                ActionTime = returnTime,
                ActionDes = "收回订单发放的积分:" + orderInfo.OSN,
                Operator = operatorId
            };

            SendCredits(partUserInfo.UserRid, creditLogInfo);
        }

        /// <summary>
        /// 发放积分
        /// </summary>
        /// <param name="userRid">用户等级id</param>
        /// <param name="creditLogInfo">积分日志信息</param>
        public static void SendCredits(int userRid, CreditLogInfo creditLogInfo)
        {
            BrnMall.Data.Credits.SendCredits(userRid, creditLogInfo);
        }

        /// <summary>
        /// 发放支付积分
        /// </summary>
        /// <param name="creditLogInfo">积分日志信息</param>
        public static void SendPayCredits(CreditLogInfo creditLogInfo)
        {
            BrnMall.Data.Credits.SendPayCredits(creditLogInfo);
        }

        /// <summary>
        /// 获得支付积分日志列表
        /// </summary>
        /// <param name="uid">用户id</param>
        /// <param name="type">类型(2代表全部类型，0代表收入，1代表支出)</param>
        /// <param name="pageSize">每页数</param>
        /// <param name="pageNumber">当前页数</param>
        /// <returns></returns>
        public static List<CreditLogInfo> GetPayCreditLogList(int uid, int type, int pageSize, int pageNumber)
        {
            return BrnMall.Data.Credits.GetPayCreditLogList(uid, type, pageSize, pageNumber);
        }

        /// <summary>
        /// 获得支付积分日志数量
        /// </summary>
        /// <param name="uid">用户id</param>
        /// <param name="type">类型(2代表全部类型，0代表收入，1代表支出)</param>
        /// <returns></returns>
        public static int GetPayCreditLogCount(int uid, int type)
        {
            return BrnMall.Data.Credits.GetPayCreditLogCount(uid, type);
        }

        /// <summary>
        /// 获得今天发放的支付积分
        /// </summary>
        /// <param name="uid">用户id</param>
        /// <param name="today">今天日期</param>
        /// <returns></returns>
        public static int GetTodaySendPayCredits(int uid, DateTime today)
        {
            return BrnMall.Data.Credits.GetTodaySendPayCredits(uid, today);
        }

        /// <summary>
        /// 获得今天发放的等级积分
        /// </summary>
        /// <param name="uid">用户id</param>
        /// <param name="today">今天日期</param>
        /// <returns></returns>
        public static int GetTodaySendRankCredits(int uid, DateTime today)
        {
            return BrnMall.Data.Credits.GetTodaySendRankCredits(uid, today);
        }

        /// <summary>
        /// 获得今天剩余的积分发放数
        /// </summary>
        /// <param name="uid">用户id</param>
        /// <param name="today">今天日期</param>
        /// <returns></returns>
        public static int GetDaySurplusPayCredits(CreditConfigInfo config, int uid, DateTime today)
        {
           
            if (config.DayMaxSendPayCredits > 0)
            {
                int todaySendCredits = GetTodaySendPayCredits(uid, today);
                if (todaySendCredits < config.DayMaxSendPayCredits)
                    return config.DayMaxSendPayCredits - todaySendCredits;
                else
                    return 0;
            }
            return -1;
        }

        /// <summary>
        /// 获得今天剩余的积分发放数
        /// </summary>
        /// <param name="uid">用户id</param>
        /// <param name="today">今天日期</param>
        /// <returns></returns>
        public static int GetDaySurplusRankCredits(CreditConfigInfo config, int uid, DateTime today)
        {
           
            if (config.DayMaxSendRankCredits > 0)
            {
                int todaySendCredits = GetTodaySendRankCredits(uid, today);
                if (todaySendCredits < config.DayMaxSendRankCredits)
                    return config.DayMaxSendRankCredits - todaySendCredits;
                else
                    return 0;
            }
            return -1;
        }

        /// <summary>
        /// 是否发放了今天的登录积分
        /// </summary>
        /// <param name="uid">用户id</param>
        /// <param name="today">今天日期</param>
        /// <returns></returns>
        public static bool IsSendTodayLoginCredit(int uid, DateTime today)
        {
            return BrnMall.Data.Credits.IsSendTodayLoginCredit(uid, today);
        }

        /// <summary>
        /// 是否发放了完成用户信息积分
        /// </summary>
        /// <param name="uid">用户id</param>
        /// <returns></returns>
        public static bool IsSendCompleteUserInfoCredit(int uid)
        {
            return BrnMall.Data.Credits.IsSendCompleteUserInfoCredit(uid);
        }

        /// <summary>
        /// 获得订单最大使用支付积分
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="payCredits">支付积分</param>
        /// <returns></returns>
        public static int GetOrderMaxUsePayCredits(int storeId, int payCredits)
        {
            var creditconfiginfo = StoreConfigManager.GetStoreCreditConfigInfo(storeId);
            if (creditconfiginfo == null) return 0;
            if (creditconfiginfo.OrderMaxUsePayCredits > 0)
                return payCredits < creditconfiginfo.OrderMaxUsePayCredits ? payCredits : creditconfiginfo.OrderMaxUsePayCredits;
            return payCredits;
        }

        /// <summary>
        /// 将支付积分转换为金钱
        /// </summary>
        /// <param name="payCredits">支付积分</param>
        /// <returns></returns>
        public static decimal PayCreditsToMoney(int storeId, int payCredits)
        {
            var creditconfiginfo = StoreConfigManager.GetStoreCreditConfigInfo(storeId);
            if (creditconfiginfo == null) return 0M;
            if (payCredits <= 0)
                return 0M;
            return (decimal)payCredits / 100 * creditconfiginfo.PayCreditPrice;
        }

        /// <summary>
        /// 将金钱转换为支付积分
        /// </summary>
        /// <param name="money">金钱</param>
        /// <returns></returns>
        public static int MoneyToPayCredits(int storeId,decimal money)
        {
            var creditconfiginfo = StoreConfigManager.GetStoreCreditConfigInfo(storeId);
            if (creditconfiginfo == null) return 0;
            if (money <= 0M)
                return 0;
            return (int)(money * 100 / creditconfiginfo.PayCreditPrice);
        }

        /// <summary>
        /// 获得用户订单发放的积分
        /// </summary>
        /// <param name="oid">订单id</param>
        /// <returns></returns>
        public static DataTable GetUserOrderSendCredits(int oid)
        {
            return BrnMall.Data.Credits.GetUserOrderSendCredits(oid);
        }
    }
}
